/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-14
 * Time: 17:31
 */

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return createTree(nums,0,nums.length-1);
    }

    private TreeNode createTree(int[] arr,int left,int right) {
        //递归结束条件
        if (left > right) {
            return null;
        }

        //拿数组中间的元素作为根结点
        int mid = (left + right)/2;
        TreeNode root = new TreeNode(arr[mid]);
        //处理左树
        root.left = createTree(arr,left,mid-1);
        //处理右树
        root.right = createTree(arr,mid+1,right);

        //返回根结点
        return root;
    }
}
